package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色管理操作
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //添加角色信息
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionsIds){
        try {
            roleService.add(role,permissionsIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
    }
    //分页查询所有

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       // System.out.println("queryPageBean ======================= " + queryPageBean);
        PageResult pageResult = roleService.findPage(queryPageBean);
        //System.out.println("pageResult =1111111111111111111111 " + pageResult);
        return pageResult;
    }
    /**
     * 根据id查询用户信息
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        //System.out.println("id ======================================== " + id);
        try {
            Role role = roleService.findById(id);
           // System.out.println("role =11111111111111111111111111111111111 " + role);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //编辑角色信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionsIds){
        try {
            roleService.edit(role,permissionsIds);
            //System.out.println("role ============================== " + role);
            System.out.println("permissionsIds = " + permissionsIds);
        }catch (Exception e){
            //e.printStackTrace();
            //System.out.println("22222222");
            return new Result(false, MessageConstant.UPDATE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.UPDATE_ROLE_SUCCESS);
    }

    //删除角色信息
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            System.out.println(" ============================== " + id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }
}
